package cn.ty.znsz.area;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import cn.ty.znsz.common.interceptor.PostInterceptor;
import cn.ty.znsz.common.model.SysArea;
import cn.ty.znsz.user.UserService;

@Path(value = "/area")
public class AreaController extends Controller{
	
	@Inject
	AreaService as;
	@Inject 
	UserService us;
	/**
	 * 得到所有地市
	 */
	public void getAllCitys() {
		Ret ret=as.getAllCitys();
		renderJson(ret);
	}
	/**
	 * 得到下一级区划
	 */
	public void searchNextArea() {
		Ret ret=as.searchNextArea(getParaToInt("areaId"));
		renderJson(ret);
	}
	/**
	 * 首页
	 */
	public void index() {
		String cookie=getCookie("jznsz");
		//查询菜单权限
		Ret ret=us.getMenuPoint(getParaToInt("id"),cookie);
		//查询用户所属区划
		Ret ret2=as.getUserArea(cookie);
		setAttr("powers", ret.get("data"));
		setAttr("areas", JsonKit.toJson(ret2.get("data")));
		render("index.html");
	}
	/**
	 * 首页分页
	 */
	public void paginate() {
		Page<SysArea> page=as.paginate(getParaToInt("pageNumber"), getParaToInt("pageSize"),getPara("city"),getPara("county"),getPara("town"),getPara("village"),
				getPara("areaName"),getPara("fullName"));
		renderJson(Ret.create().set("msg", "操作成功").set("code", 0).set("count", page.getTotalRow()).set("data", page.getList()));
	}
	/**
	 * 跳转新增页
	 */
	public void areaAdd() {
		String cookie=getCookie("jznsz");
		//查询用户所属区划
		Ret ret=as.getAreasById(cookie);
		setAttr("areas", JsonKit.toJson(ret.get("data")));
		render("areaAdd.html");
	}
	/**
	 * 区划保存
	 */
	@Before({PostInterceptor.class,AreaValidator.class})
	public void areaSave() {
		SysArea data=getModel(SysArea.class, "data");
		Ret ret=as.areaSave(data,getParaToInt("parentId"));
		renderJson(ret);
	}
	/**
	 * 跳转编辑
	 */
	public void areaEdit() {
		String cookie=getCookie("jznsz");
		//查询用户所属区划
		Ret ret=as.getAreasById(cookie);
		setAttr("areas", JsonKit.toJson(ret.get("data")));
		render("areaEdit.html");
	}
	/**
	 * 区划修改
	 */
	@Before({PostInterceptor.class,AreaValidator.class})
	public void areaUpdate() {
		SysArea data=getModel(SysArea.class, "data");
		Ret ret=as.areaUpdate(data,getParaToInt("id"),getParaToInt("parentId"));
		renderJson(ret);
	}
	/**
	 * 区划删除
	 */
	@Before(PostInterceptor.class)
	public void areaDel() {
		Ret ret=as.areaDel(getParaToInt("id"));
		renderJson(ret);
	}
}
